package com.animal.controller;

import com.animal.bean.DailyBean;
import com.animal.dao.Connect;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class InsertServlet extends HttpServlet{
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * Constructor of the object.
     */
    public InsertServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        DailyBean d = new DailyBean();
        d.setFileNumber(request.getParameter("filenumber"));
        d.setFileTitle(request.getParameter("FileName"));
        d.setUserName("日常档案管理员");
        d.setAnimalID(request.getParameter("AnimalID"));
        d.setDailyDataTip(request.getParameter("Tip"));
        d.setFileState("已审核");

        if(d.getAnimalID()==null||d.getAnimalID()==""){
            out.print("<script>alert('动物编号不能为空!');window.location='DailyFileUpdate.jsp';</script>");
        }
        if(d.getFileTitle()==null||d.getFileTitle()==""){
            out.print("<script>alert('档案名称不能为空!');window.location='DailyFileUpdate.jsp';</script>");
        }

        Connection con =  Connect.getCon();
        try{
            String sql = "UPDATE animalfile09_dailyarchive SET FileTitle='"+d.getFileTitle()+"',FillingDate=NOW(),AnimalID='"+d.getAnimalID()+"',FileState='已审核',DailyDataTip='"+d.getDailyDataTip()+"' WHERE FileNumber='"+d.getFileNumber()+"' ;";
            Statement stmt = null ;
            stmt = con.createStatement() ;    // 实例化Statement对象
            stmt.executeUpdate(sql);
            con.close();
        }catch(SQLException e){
            e.printStackTrace();
            System.out.println("更新信息出错！");
        }catch(Exception e){
            e.printStackTrace();
        }
        finally{
            System.out.println("结束.");
        }
        out.print("<script language='javascript' >alert('档案更新成功!');window.location='DailyFileManagement.jsp';</script>");


    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        doGet(request,response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }
}
